function getFile(filename, fn) {
    plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, (fs) => {

        fs.root.getFile(filename, {create: true}, (fileEntry) => {

            fileEntry.file((file) => {

                var fileReader = new plus.io.FileReader();
                fileReader.readAsText(file, 'utf-8');
                fileReader.onloadend = (evt) => {

                    var result = evt.target.result;
                    let data = null;
                    if (result !== "") {
                        data = JSON.parse(result);
                    }
                    fn({data: data, entry: fileEntry});
                }
            });
        });
    }, function (e) {
        alert(e)
    });
}

// 读取文件
function readFile(filename, fn) {
    getFile(filename, (param) => {
        fn(param);
    });
}

// 写入文件
function writeFile(filename, fn) {
    readFile(filename, ({data, entry}) => {
        entry.createWriter((writer) => {
            fn({data, writer, error: false});
        }, function (e) {
            fn({error: true, mes: JSON.stringify(e)});
        });
    });
}


function resolve(path, fn) {
    plus.io.resolveLocalFileSystemURL(path, entry => {
        fn(entry);
    }, e => {
        //读取文件或目录失败
        console.log(JSON.stringify(e));
    })
}

// 删除文件夹
function removeDirectory(path, fn) {
    resolve(path, entry => {
        entry.removeRecursively(() => {
            fn(path);
        }, () => {
            // 删除失败
            console.log('删除失败', path);
            fn();
        });
    });
}

// 删除文件
function removeFile(path, fn) {
    resolve(path, entry => {
        entry.remove(() => {
            fn(path);
        }, () => {
            // 删除失败
            console.log('删除失败', path);
            fn();
        });
    });
}

// 判断文件是否存在
function fileExist(filename, prepath = '_documents/') {
    return new Promise((resolve) => {
        plus.io.resolveLocalFileSystemURL(
            prepath + filename,
            function (entry) {
                if (entry.isFile) {
                    resolve(true);
                } else {
                    resolve(false);
                }
            },
            function (e) {
                resolve(false);
                console.log(JSON.stringify(e));
            }
        );
    });
}

// 读取图片
function getImage(imgName, fn) {
    plus.io.resolveLocalFileSystemURL(`_documents/photos/${imgName}.cif`, entry => {
        entry.file((file) => {
            var fileReader = new plus.io.FileReader();
            fileReader.readAsText(file, 'utf-8');
            fileReader.onloadend = (evt) => {
                var result = evt.target.result;
                if (result !== "") {
                    fn(result);
                } else {
                    fn('');
                }
            }
        });
    }, e => {
        //读取文件或目录失败
        console.log(JSON.stringify(e));
        fn('');
    });

    // plus.io.getImageInfo({
    //     src: `_documents/photos/${imgName}.jpg`,
    //     success(res) {
    //         fn(res.path);
    //     },
    //     fail() {
    //         alert('图片读取失败');
    //     }
    // });
}
function closeDB(name){
    return new Promise((resolve, reject) => {
        plus.sqlite.closeDatabase({
            // name: 'first',
            name: name,
            success: function(){
                console.log('关闭成功');
                resolve();
            },
            fail: function(e){
                reject();
                console.log(JSON.stringify(e));
            }
        });
    });
}
export const IO = {
    writeFile,
    readFile,
    fileExist,
    getImage,
    removeDirectory,
    removeFile,
    closeDB
};
